/*
 *  This file is part of AtlasMapper server and clients.
 *
 *  Copyright (C) 2012 Australian Institute of Marine Science
 *
 *  Contact: Gael Lafond <g.lafond@aims.org.au>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package au.gov.colouriseimage.servlet;

import au.gov.aims.colouriseimage.Colouriser;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class ImageGenerator {
	private File tmpFolder = null;

	/**
	 * -----------------------------------------------------------------------------
	 * | Constant            | Value | File type                 | Supported       |
	 * -----------------------------------------------------------------------------
	 * | TYPE_CUSTOM         | 0     | Grayscale PNG             | Yes (converted) |
	 * | TYPE_INT_RGB        | 1     | BMP 32 bits (A8 R8 G8 B8) | Yes             |
	 * | TYPE_INT_ARGB       | 2     | ?                         | Probably        |
	 * | TYPE_INT_ARGB_PRE   | 3     | ?                         | Probably        |
	 * | TYPE_INT_BGR        | 4     | ?                         | Probably        |
	 * | TYPE_3BYTE_BGR      | 5     | JPEG                      | Yes             |
	 * | TYPE_4BYTE_ABGR     | 6     | PNG                       | Yes             |
	 * | TYPE_4BYTE_ABGR_PRE | 7     | ?                         | Probably        |
	 * | TYPE_USHORT_565_RGB | 8     | ? (*)                     | Yes             |
	 * | TYPE_USHORT_555_RGB | 9     | BMP 16 bits (X1 R5 G5 B5) | Yes             |
	 * | TYPE_BYTE_GRAY      | 10    | Grayscale JPEG            | Yes (converted) |
	 * | TYPE_USHORT_GRAY    | 11    | ?                         | Yes (converted) |
	 * | TYPE_BYTE_BINARY    | 12    | ?                         | Yes (converted) |
	 * | TYPE_BYTE_INDEXED   | 13    | GIF, Indexed PNG          | Yes             |
	 * -----------------------------------------------------------------------------
	 *
	 * (*) I though this one would be "BMP 16 bits (R5 G6 B5)", but java do not support this BMP format.
	 */
	public static void main(String[] args) throws IOException {
		BufferedImage origImg = ImageGenerator.getOrigImg();
		ImageGenerator generator = new ImageGenerator();

		// BMP => 5, PNG => 5, JPG => 5
		//BufferedImage type1 = Colouriser.convert(origImg, BufferedImage.TYPE_INT_RGB);
		//generator.saveGeneratedImg(type1, "bmp", "TYPE_INT_RGB.bmp");

		// PNG => 6, JPG => 0, BMP => empty file
		//BufferedImage type2 = Colouriser.convert(origImg, BufferedImage.TYPE_INT_ARGB);
		//generator.saveGeneratedImg(type2, "png", "TYPE_INT_ARGB.png");

		// PNG => 6, JPG => 0, BMP => empty file
		//BufferedImage type3 = Colouriser.convert(origImg, BufferedImage.TYPE_INT_ARGB_PRE);
		//generator.saveGeneratedImg(type3, "png", "TYPE_INT_ARGB_PRE.png");

		// BMP => 5, PNG => 5, JPG => 5
		//BufferedImage type4 = Colouriser.convert(origImg, BufferedImage.TYPE_INT_BGR);
		//generator.saveGeneratedImg(type4, "bmp", "TYPE_INT_BGR.bmp");

		BufferedImage type5 = Colouriser.convert(origImg, BufferedImage.TYPE_3BYTE_BGR);
		generator.saveGeneratedImg(type5, "jpg", "TYPE_3BYTE_BGR.jpg");

		BufferedImage type6 = Colouriser.convert(origImg, BufferedImage.TYPE_4BYTE_ABGR);
		generator.saveGeneratedImg(type6, "png", "TYPE_4BYTE_ABGR.png");

		// PNG => 6. JPG => 0, BMP => empty file, GIF => 13
		//BufferedImage type7 = Colouriser.convert(origImg, BufferedImage.TYPE_4BYTE_ABGR_PRE);
		//generator.saveGeneratedImg(type7, "png", "TYPE_4BYTE_ABGR_PRE.png");

		BufferedImage type8 = Colouriser.convert(origImg, BufferedImage.TYPE_USHORT_565_RGB);
		generator.saveGeneratedImg(type8, "bmp", "TYPE_USHORT_565_RGB.bmp");

		BufferedImage type9 = Colouriser.convert(origImg, BufferedImage.TYPE_USHORT_555_RGB);
		generator.saveGeneratedImg(type9, "bmp", "TYPE_USHORT_555_RGB.bmp");

		BufferedImage type10 = Colouriser.convert(origImg, BufferedImage.TYPE_BYTE_GRAY);
		generator.saveGeneratedImg(type10, "jpg", "TYPE_BYTE_GRAY.jpg");

		BufferedImage type11 = Colouriser.convert(origImg, BufferedImage.TYPE_USHORT_GRAY);
		generator.saveGeneratedImg(type11, "png", "TYPE_USHORT_GRAY.png");

		BufferedImage type12 = Colouriser.convert(origImg, BufferedImage.TYPE_BYTE_BINARY);
		generator.saveGeneratedImg(type12, "bmp", "TYPE_BYTE_BINARY.bmp");

		BufferedImage type13 = Colouriser.convert(origImg, BufferedImage.TYPE_BYTE_INDEXED);
		generator.saveGeneratedImg(type13, "gif", "TYPE_BYTE_INDEXED.gif");

		System.out.println("All images has been generated in the folder: " + generator.getTempFolder().getAbsolutePath());
	}

	private static BufferedImage getOrigImg() throws IOException {
		return ImageIO.read(TestColouriseImage.class.getClassLoader().getResourceAsStream("colourise/orig.png"));
	}

	private void saveGeneratedImg(BufferedImage image, String format, String filename) throws IOException {
		File file = new File(this.getTempFolder(), filename);
		FileOutputStream output = null;
		try {
			output = new FileOutputStream(file);
			ImageIO.write(image, format, output);
		} finally {
			if (output != null) {
				output.close();
			}
		}
	}

	private File getTempFolder() {
		if (this.tmpFolder == null) {
			File tmpDir = new File(System.getProperty("java.io.tmpdir"));
			this.tmpFolder = new File(tmpDir, "ColouriseImage_generated");
			this.tmpFolder.mkdirs();
		}
		return this.tmpFolder;
	}
}
